//
//  FilterViewController.m
//  OfficeBook
//
//  Created by 孙晓奇 on 15/10/1.
//  Copyright © 2015年 sunny. All rights reserved.
//

#import "FilterViewController.h"
#import "FilterFavorViewCell.h"
#import "CapacityFilterViewController.h"
#import "EquipmentFilterViewController.h"
#import "PresentModalHelper.h"
#import "SelectCityViewController.h"
#import "FilterModel.h"
#import "LocationModel.h"

static NSString * const kFavorCellIdentifer = @"FavorCellIdentifer";
static NSString * const kDefaultCellIdentifer = @"DefaultCellIdentifer";

@interface FilterViewController ()
<UITableViewDelegate, UITableViewDataSource, SelectCityDelegate, CapacityFilterControllerDelegate, EquipmentFilterControllerDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) FilterModel *filterInfo;

@end

@implementation FilterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ebebeb"];
    
    [self initNavBarItem];
    [self initTableView];
    
    self.filterInfo = [[FilterModel alloc] init];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - layout
-(void)initNavBarItem {
    self.navigationItem.title = @"Filter";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(save:)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];
}

// save
-(void)save:(UIButton *)sender {
    [self.delegate viewController:self didFinishFiltration:self.filterInfo];
    [self dismissViewControllerAnimated:YES completion:nil];
}

// cancel
-(void)cancel:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - tableview dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rows;
    
    switch (section) {
        case 0:
        case 1:
            rows = 1;
            break;
        case 2:
            rows = 2;
            break;
        default: break;
    }
    return rows;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        FilterFavorViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFavorCellIdentifer];
        if (cell == nil) {
            cell = [[FilterFavorViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kFavorCellIdentifer];
        }
        cell.cellSize = CGSizeMake(self.tableView.frame.size.width, [self tableView:tableView heightForRowAtIndexPath:indexPath]);
        cell.favorItems = @[@"favor 1", @"favor 2", @"favor 3", @"favor 4", @"favor 5", @"favor 6"];
        return  cell;
    }
    else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDefaultCellIdentifer];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kDefaultCellIdentifer];
        }
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.section == 0) {
            cell.textLabel.text = @"Location";
            cell.detailTextLabel.text = @"Select city";
        } else if (indexPath.section == 2 && indexPath.row == 0){
            cell.textLabel.text = @"Capacity";
            cell.detailTextLabel.text = @"All";
        } else if (indexPath.section == 2 && indexPath.row == 1){
            cell.textLabel.text = @"Equipment";
            cell.detailTextLabel.text = @"All";
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SelectCityViewController *selectCityVC = [[SelectCityViewController alloc] init];
        selectCityVC.delegate = self;
        [self.navigationController pushViewController:selectCityVC animated:YES];
    }
    else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            CapacityFilterViewController *capacityFilterVC= [[CapacityFilterViewController alloc] init];
            capacityFilterVC.delegate = self;
            if (self.filterInfo.capacity != nil && [self.filterInfo.capacity count] > 0) {
                capacityFilterVC.selectedItemsIdxSet = [[NSMutableIndexSet alloc] initWithIndexSet:self.filterInfo.capacity];
            }
            [self.navigationController pushViewController:capacityFilterVC animated:YES];
        }
        else if (indexPath.row == 1) {
            EquipmentFilterViewController *equipmentFilterVC= [[EquipmentFilterViewController alloc] init];
            equipmentFilterVC.delegate = self;
            if (self.filterInfo.equipment != nil && [self.filterInfo.equipment count] > 0) {
                equipmentFilterVC.selectedItemsIdxSet = [[NSMutableIndexSet alloc] initWithIndexSet:self.filterInfo.equipment];
            }
            [self.navigationController pushViewController:equipmentFilterVC animated:YES];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 12.0;
    } else {
        return 0.1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 12.0;
    } else {
        return 0.1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return 90.f;
    }
    return 44.f;
}

#pragma mark -SelectCityDelegate
-(void)viewController:(SelectCityViewController *)controller didSelectLocation:(LocationModel *)location{
    //NSLog(@"选择的城市:%@", city);
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    cell.detailTextLabel.text = location.name;
    self.filterInfo.location = location;
}

#pragma mark - CapacityFilterControllerDelegate
-(void)viewController:(CapacityFilterViewController *)controller didSelectCapacity:(NSIndexSet *)capacity{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    NSArray *capAry = [controller.titleArray objectsAtIndexes:capacity];
    cell.detailTextLabel.text = [[capAry valueForKey:@"description"] componentsJoinedByString:@","];

    self.filterInfo.capMin = 2 * capacity.firstIndex;
    if (capacity.lastIndex != 0 && capacity.lastIndex != controller.titleArray.count - 1) {
        self.filterInfo.capMax = 2 * capacity.lastIndex + 2;
    } else {
        self.filterInfo.capMax = 10000;   // max 10000 persons
    }
    self.filterInfo.capacity = capacity;
}

#pragma mark - CapacityEquipmentControllerDelegate
-(void)viewController:(CapacityFilterViewController *)controller didSelectEquipment:(NSIndexSet *)equipment {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:2];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    NSArray *equipAry = [controller.titleArray objectsAtIndexes:equipment];
    cell.detailTextLabel.text = [[equipAry valueForKey:@"description"] componentsJoinedByString:@","];
#warning fixme hard code
    self.filterInfo.equipStr = (equipment.firstIndex == 0? @"VC,Projector,Screen,Phone": cell.detailTextLabel.text);
    self.filterInfo.equipment = equipment;
}

@end
